<?php
require_once 'DataBaseObject.php';

/**
 * Description of Transaction
 *
 * @author josema
 */
class Transaction extends DataBaseObject implements FormComunication{
    protected $table = 'economy';

    protected $key = 'id';

    protected $foreign_keys = array(
        'owner' => array('class' => 'User', 'path' => 'users', 'cardinality' => 1)
    );

    public function formLoad($prefix = false, $sufix = false) {
        $load_prop = array('id' => $prefix.'id'.$sufix,
            'description' => $prefix.'description'.$sufix,
            'amount' => $prefix.'amount'.$sufix,
            'account' => $prefix.'account'.$sufix,
            'type' => $prefix.'type'.$sufix,
            'date' => $prefix.'date'.$sufix,
            'label' => $prefix.'label'.$sufix,
            'going_to' => $prefix.'to'.$sufix);

        $return = false;

        foreach ($load_prop as $prop => $input_name){
            $value = Input::get($input_name);
            $this->$prop = $value;
            $return = $return || $value;
        }

        return $return;
    }

    public function showForm($wrap = true, $prefix = false, $sufix = false) {
        $user = get_user();

        $accounts = $user->getRelation('accounts');
        $acc_options = array();
        $acc_options[0] = '';
        foreach ($accounts as $acc)
            $acc_options[$acc->id] = $acc->name;
        
        $show_prop = array(
            'id' => array('type' => 'hidden', 'id' => 'id',
                'name' => $prefix.'id'.$sufix, 'label' => null),
            'description' => array('type' => 'text', 'id' => 'description',
                'name' => $prefix.'description'.$sufix, 'label' => 'Descripcion'),
            'amount' => array('type' => 'text', 'id' => 'amount',
                'name' => $prefix.'amount'.$sufix, 'label' => 'Amount'),
            'type' => array('type' => 'select', 'id' => 'type',
                'name' => $prefix.'type'.$sufix, 
                'options'=> array('Expense' => 'Expense', 'Income' => 'Income',
                    'Refund' => 'Refund', 'Transfer' => 'Transfer'),
                'label' => 'Type'),
            'account' => array('type' => 'select', 'id' => 'account',
                'name' => $prefix.'account'.$sufix, 'options' => $acc_options, 'label' => 'Account'),
            'to' => array('type' => 'select', 'id' => 'to',
                'name' => $prefix.'to'.$sufix, 'options' => $acc_options, 'label' => 'To'),
            'date' => array('type' => 'text', 'id' => 'date',
                'name' => $prefix.'date'.$sufix, 'label' => 'Fecha'),
            'label' => array('type' => 'text', 'id' => 'label',
                'name' => $prefix.'label'.$sufix, 'label' => 'Etiqueta')/*,
            'save' => array('type' => 'submit', 'id' => 'save',
                'name' => 'save', 'value' => 'Save', 'label' => null)*/
            );
        
        foreach ($show_prop as $key => $data) {
            if(!isset($show_prop[$key]['value'])) $show_prop[$key]['value'] = $this->{$key};
        }

        $action = 'add';

        return Form::generate($show_prop, $action, $wrap);
    }

    public function formatAmount(){
        // If is an Income transaction, the amount must be possitivie, else negative
        if($this->type == 'Income' || $this->type == 'Refund')
            $this->amount = abs($this->amount);
        else
            $this->amount = - abs($this->amount);
    }
}
?>
